<?php
class Storefront_CustomerController extends Zend_Controller_Action
{
    /**
     * An instance of user model
     *
     * @var Storefront_Model_User
     */
    protected $_model;
    
    /**
     * An instance of Authentication service
     *
     * @var Storefront_Service_Authentication
     */
    protected $_authService;

    public function init()
    {
        
        // get default model
        $this->_model = new Storefront_Model_User();
        
        // get auth service
        $this->_authService = new Storefront_Service_Authentication();
        
        // add forms
        $this->view->registerForm = $this->getRegistrationForm();
        $this->view->loginForm = $this->getLoginForm();
        $this->view->userForm = $this->getUserForm();
        
    }
    
    public function loginAction()
    {}
    
    public function authenticateAction()
    {
        $request = $this->getRequest();
        
        if (!$request->isPost()) {
            return $this->_helper->redirector('login');
        }
        
        // Validate
        $form = $this->_forms['login'];
        if (!$form->isValid($request->getPost())) {
            return $this->render('login');
        }
        if (FALSE === $this->_authService->authenticate($form->getValues())) {
            $form->setDescription('Login failed! Please try again.');
            return $this->render('login');
        }
        return $this->_helper->redirector('index');
    }
    
    public function logoutAction()
    {
        $this->_authService->clear();
        return $this->_helper->redirector('index');
    }
    
    public function getLoginForm()
    {
        $urlHelper = $this->_helper->getHelper('url');
        $this->_forms['login'] = $this->_model->getForm('userLogin');
        $this->_forms['login']->setAction($urlHelper->url(array(
            'controller'    => 'customer',
            'action'        => 'authenticate',
            ),
            'default'        
        ));
        $this->_forms['login']->setMethod('post');
        
        return $this->_forms['login'];
    }

    /**
     * Display user edit form
     */
    public function indexAction()
    {
       
        $userId = 1; // will be from session
        
        $this->view->user = $this->_model->getUserById($userId);
        $this->view->userForm = $this->getUserForm()->populate(
                                    $this->view->user->toArray()
                                );
        
    }
    
    /**
     * Update user information
     * 
     * @return type 
     */
    public function saveAction()
    {
        
        $request = $this->getRequest();
        
        if(!$request->isPost()) {
            return $this->_helper->redirector('index');
        }
        
        if(FALSE === $this->_model->saveUser($request->getPost())) {
            return $this->render('index');
        }   
        
        
    }
    
    /**
     * Simply render the register view
     */
    public function registerAction()
    {}
    
    /**
     * 
     *
     * @return type 
     */
    public function completeRegistrationAction()
    {
        
        $request = $this->getRequest();
        
        if(!$request->isPost()) {
            return $this->_helper->redirector('register');
        }
        
        if(FALSE === ($id = $this->_model->registerUser($request->getPost()))) {
            return $this->render('register');
        }   
        
    }
    
    public function getRegistrationForm()
    {
        
        $urlHelper = $this->_helper->getHelper('url');
        
        $this->_forms['register'] = $this->_model->getForm('userRegister');
        
        $this->_forms['register']->setAction($urlHelper->url(array(
                                        'controller' => 'customer',
                                        'action' => 'complete-registration',
                                        ),
                                        'default'    
        ));
        
        $this->_forms['register']->setMethod('post');
        
        return $this->_forms['register'];
        
        
        
    }
    
    public function getUserForm()
    {
        
        $urlHelper = $this->_helper->getHelper('url');
        
        $this->_forms['userEdit'] = $this->_model->getForm('userEdit');
        
        $this->_forms['userEdit']->setAction($urlHelper->url(array(
                                        'controller' => 'customer',
                                        'action' => 'save',
                                        ),
                                        'default'    
        ));
        
        $this->_forms['userEdit']->setMethod('post');
        
        return $this->_forms['userEdit'];
        
    }

    
}

